import { defineStore } from "pinia";
import { Message } from "@arco-design/web-vue";
import * as couponBaseApi from "@/api/coupon-base";

export const useCouponBaseStore = defineStore("couponBase", () => {
  async function getCoupons(param?: any) {
    const response = await couponBaseApi.getCoupons(param);
    if (response.code === 20000) {
      return response.data.coupons;
    }
    return [];
  }

  async function getCoupon(param?: any) {
    const response = await couponBaseApi.getCoupon(param);
    if (response.code === 20000) {
      return response.data.coupon;
    }
    return {};
  }

  async function saveCoupon(param?: any) {
    const apiCall = param.id
      ? couponBaseApi.updateCoupon
      : couponBaseApi.createCoupon;
    const response = await apiCall({ ...param });
    if (response.code === 20000) {
      Message.success(param.id ? "更新成功" : "新增成功");
    }
  }

  async function deleteCoupon(param?: any) {
    const response = await couponBaseApi.deleteCoupon(param);
    if (response.code === 20000) {
      Message.success("移除成功");
    }
  }

  return { getCoupons, getCoupon, saveCoupon, deleteCoupon };
});
